#ifndef __Y64_LIB_H__
#define __Y64_LIB_H__

#define NULL 0

#define container_of(ptr, type, member)	\
({	\
	typeof(((type *)0)->member) *p = (ptr);	\
	(type *)((unsigned long)p - (unsigned long)&(((type *)0)->member));	\
})

#define STI() __asm__ __volatile__ ("sti \n\t":::"memory")				// 恢复中断
#define CLI() __asm__ __volatile__ ("cli \n\t":::"memory")				// 禁止中断
#define NOP() __asm__ __volatile__ ("nop \n\t")							// 空操作
#define IO_MFENCE() __asm__ __volatile__ ("mfence \n\t":::"memory")		// 内存屏障

// 从I/O端口port中读取nr个字(2字节)到buffer
#define PORT_INSW(port, buffer, nr)	__asm__ __volatile__("cld;rep;insw;mfence;"::"d"(port), "D"(buffer), "c"(nr):"memory")
// 将buffer中nr个字（2字节）数据写入I/O端口port
#define PORT_OUTSW(port, buffer, nr) __asm__ __volatile__("cld;rep;outsw;mfence;"::"d"(port), "S"(buffer), "c"(nr):"memory")

// 双向链表
typedef struct __st_list {
	struct __st_list *prev;
	struct __st_list *next;
} st_list;

inline void list_init(st_list *list);

inline void list_add_to_behind(st_list *entry, st_list *new);

inline void list_add_to_before(st_list *entry, st_list *new);

inline void list_del(st_list *entry);

inline long list_is_empty(st_list *entry);

inline st_list *list_prev(st_list *entry);

inline st_list *list_next(st_list *entry);

inline int strlen(char *str);

inline void *memset(void *addr, unsigned char c, long count);

inline void *memcpy(void *from, void *to, long count);

inline unsigned char io_in8(unsigned short port);

inline unsigned int io_in32(unsigned short port);

inline void io_out8(unsigned short port, unsigned char value);

inline void io_out32(unsigned short port, unsigned int value);

inline void wrmsr(unsigned long addr, unsigned long value);

#endif